import { mount } from '@vue/test-utils';
import { IonicVue } from '@ionic/vue';
import HomePage from '@/views/HomePage.vue';

describe('HomePage.vue', () => {
  let wrapper: any;

  beforeEach(() => {
    wrapper = mount(HomePage, {
      global: {
        plugins: [IonicVue]
      }
    });
  });

  afterEach(() => {
    wrapper.unmount();
  });

  describe('组件渲染', () => {
    it('应该正确渲染页面标题', () => {
      expect(wrapper.find('ion-title').exists()).toBe(true);
    });

    it('应该显示搜索栏', () => {
      expect(wrapper.find('ion-searchbar').exists()).toBe(true);
    });

    it('应该显示图书分类段控制器', () => {
      expect(wrapper.find('ion-segment').exists()).toBe(true);
    });
  });

  describe('数据展示', () => {
    it('应该显示编程类图书', () => {
      expect(wrapper.vm.data1).toBeDefined();
      expect(wrapper.vm.data1.length).toBeGreaterThan(0);
    });

    it('应该显示小说类图书', () => {
      expect(wrapper.vm.data2).toBeDefined();
      expect(wrapper.vm.data2.length).toBeGreaterThan(0);
    });

    it('应该显示推荐图书', () => {
      expect(wrapper.vm.data4).toBeDefined();
      expect(wrapper.vm.data4.length).toBeGreaterThan(0);
    });
  });

  describe('用户交互', () => {
    it('应该能够切换图书分类', async () => {
      const segment = wrapper.find('ion-segment');
      expect(segment.exists()).toBe(true);
      
      // 模拟分类切换
      wrapper.vm.svalue = '小说';
      await wrapper.vm.$nextTick();
      
      expect(wrapper.vm.svalue).toBe('小说');
    });

    it('应该响应搜索输入', async () => {
      const searchbar = wrapper.find('ion-searchbar');
      expect(searchbar.exists()).toBe(true);
      
      // 模拟搜索事件
      await searchbar.trigger('ionChange', {
        detail: { value: 'Java' }
      });
      
      // 验证搜索逻辑
      expect(searchbar.exists()).toBe(true);
    });
  });

  describe('性能测试', () => {
    it('组件挂载时间应该在合理范围内', () => {
      const startTime = performance.now();
      const testWrapper = mount(HomePage, {
        global: {
          plugins: [IonicVue]
        }
      });
      const endTime = performance.now();
      
      expect(endTime - startTime).toBeLessThan(100); // 100ms内完成挂载
      testWrapper.unmount();
    });
  });
});